//
//  InnerWebViewController.swift
//  MintBag
//
//  Created by wy on 2024/9/9.
//

import UIKit
import WebKit

class InnerWebViewController: UIViewController, WKNavigationDelegate {

    var url: URL!
    var webView: WKWebView!

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white

        let config = WKWebViewConfiguration()
        let userContentController = WKUserContentController()
        config.userContentController = userContentController
        webView = WKWebView(frame: self.view.frame, configuration: config)
        webView.navigationDelegate = self
        self.view.addSubview(webView)
        webView.addObserver(self, forKeyPath: "title", options: .new, context: nil)

        webView.load(URLRequest(url: url))
    }
    
    deinit {
        webView.removeObserver(self, forKeyPath: "title", context: nil)
    }
    
    override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey: Any]?, context: UnsafeMutableRawPointer?) {
        guard let tempKeyPath = keyPath else { return }
        if tempKeyPath == "title" {
            navigationItem.title = webView.title
        }
    }

}
